require 'test/unit'
require 'watir'
         
         #~ $un="crux@eforceglobal.com"
         #~ $pwd="wafer"
         #~ $ip="10.7.10.178"
         #~ $ie = Watir::IE.start('http://'+$ip+'/cruxWebUI/CruxHome.aspx')
         #~ $filepath="E:/Crux_Automation"

    class TC_01Company < Test::Unit::TestCase  
 
  def test_01Login
  
         require 'watir'

        puts "User is going to login to the Crux Application"
        sleep 5
        $ie.text_field(:name,"ctl00$ContentPlaceHolder1$txtUserName$txtWaterMarkedTextBox").set $un
        $ie.text_field(:name,"ctl00$ContentPlaceHolder1$txtPassword$txtWaterMarkedTextBox").set $pwd
        $ie.button(:name,"ctl00$ContentPlaceHolder1$btnLogin").click
        puts "Successfully logged into the Application"
        sleep 10

    end

 def test_02AddCompany  
  
  require 'xls'  
  require 'watir'

   xlFile = XLS.new($filepath + "/Crux_Data/Add_Company.xls") #grab the data file in the same dirrectory

   myData = xlFile.getRowRecords('','Sheet1')  #pull data records  from excel
   xlFile.close

   puts "*************************"
   puts "Add Company Functionality"
   puts "*************************"


   $ie.link(:text,"Administration").click
   sleep 5
   $ie.link(:text,"Manage Companies").click
   sleep 5
   myData.each do |record|
  
  puts "Creating a Company : '#{record['CompanyName']}'"  
  $ie.button(:name,"ctl00$ContentPlaceHolder1$btnAddNewCompany").click
  sleep 5 
  $ie.text_field(:id, 'ctl00_ContentPlaceHolder1_txtCompanyName_txtWaterMarkedTextBox').set(record['CompanyName'])
  $ie.text_field(:id, 'ctl00_ContentPlaceHolder1_txtCompanyBillingAddress_txtWaterMarkedTextBox').set(record['Baddress'])
  $ie.text_field(:id, 'ctl00_ContentPlaceHolder1_txtCompanyMailingAddress_txtWaterMarkedTextBox').set(record['Maddress']) 
  #~ $ie.select_list(:id, 'ctl00_ContentPlaceHolder1_drpCompanyType').select_value(record['CompanyType'])
  $ie.select_list(:id, 'ctl00_ContentPlaceHolder1_drpCompanyType').select(record['CompanyType'])
  $ie.text_field(:id, 'ctl00_ContentPlaceHolder1_txtCompanyComments_txtWaterMarkedTextBox').set(record['Comments']) 
  $ie.button(:id,"ctl00_ContentPlaceHolder1_btnSave").click
  sleep 5  
 
  puts "Company : '#{record['CompanyName']}'created successfully " 
  puts "Verifying created Company :'#{record['CompanyName']}'"  
  b=$ie.table(:id,"ctl00_ContentPlaceHolder1_grdCompanies").row_count

     
     for i in 2..b do
     a=$ie.table(:id,"ctl00_ContentPlaceHolder1_grdCompanies")[i][2].text 
     #puts a
     if a=="#{record['CompanyName']}" 
     puts "Company: '#{record['CompanyName']}' verified successfully"   
     break
      end
        end
      end
      
 end

     def test_03EditCompany
     
     require 'xls'
     require 'watir'

     xlFile = XLS.new($filepath + "/Crux_Data/Edit_Company.xls") #grab the data file in the same dirrectory
     myData = xlFile.getRowRecords('','Sheet1')  #pull data records  from excel
     xlFile.close

     puts "**************************"
     puts "Edit Company Functionality"
     puts "**************************"

     $ie.link(:text,"Administration").click
     sleep 5

     $ie.link(:text,"Manage Companies").click
     sleep 5

     myData.each do |record|
  
     puts "Verifying existing Company : '#{record['CompanyName']}'for Update"   
 
     b=$ie.table(:id,"ctl00_ContentPlaceHolder1_grdCompanies").row_count
   
     #puts b
     
     for i in 2..b do
     a=$ie.table(:id,"ctl00_ContentPlaceHolder1_grdCompanies")[i][2].text 
    #puts a
    if a=="#{record['CompanyName']}"      
     puts "Company: '#{record['CompanyName']}' verified successfully"   
    sleep 5
     puts " Updating the Company :'#{record['CompanyName']}' as '#{record['ECompanyName']}'"
     $ie.table(:id,"ctl00_ContentPlaceHolder1_grdCompanies")[i][7].button(:src,"http://"+$ip+"/CruxWebUI/appimages/Edit.gif").click
    sleep 5
    $ie.text_field(:id, 'ctl00_ContentPlaceHolder1_txtCompanyName_txtWaterMarkedTextBox').set(record['ECompanyName'])
    $ie.text_field(:id, 'ctl00_ContentPlaceHolder1_txtCompanyBillingAddress_txtWaterMarkedTextBox').set(record['EBaddress'])
    $ie.text_field(:id, 'ctl00_ContentPlaceHolder1_txtCompanyMailingAddress_txtWaterMarkedTextBox').set(record['EMaddress']) 
    $ie.text_field(:id, 'ctl00_ContentPlaceHolder1_txtCompanyComments_txtWaterMarkedTextBox').set(record['EComments']) 
    $ie.button(:id,"ctl00_ContentPlaceHolder1_btnSave").click
    sleep 8
 
     puts "Company :'#{record['CompanyName']}' successfully updated as '#{record['ECompanyName']}'" 
     puts "verifying the updated Company: '#{record['ECompanyName']}'"
 
     b=$ie.table(:id,"ctl00_ContentPlaceHolder1_grdCompanies").row_count

     for i in 2..b do
     a=$ie.table(:id,"ctl00_ContentPlaceHolder1_grdCompanies")[i][2].text 
    #puts a
    if a=="#{record['ECompanyName']}"      
      puts "Updated Company :'#{record['ECompanyName']}' verified successfully"
      break
        end
         end  
          break  
          end   
           end
               end
    
  end   

  
  def test_04DeleteCompany
 
   require 'xls'
   require 'watir'
   require 'watir/ie' 
   require 'watir/contrib/enabled_popup'


   xlFile = XLS.new($filepath + "/Crux_Data/Delete_Company.xls") #grab the data file in the same dirrectory
   myData = xlFile.getRowRecords('','Sheet1')  #pull data records  from excel
   xlFile.close

   puts "***************************"
   puts "Delete Company Functionality"
   puts "***************************"


   $ie.link(:text,"Administration").click
   sleep 5

   $ie.link(:text,"Manage Companies").click
   sleep 5

   myData.each do |record|
  
   puts "Verifying a Company : '#{record['CompanyName']}'for Delete"   
 
   b=$ie.table(:id,"ctl00_ContentPlaceHolder1_grdCompanies").row_count
 
     for i in 2..b do
     a=$ie.table(:id,"ctl00_ContentPlaceHolder1_grdCompanies")[i][2].text 

     if a=="#{record['CompanyName']}"      
     puts "Company: '#{record['CompanyName']}' verified successfully"   
     sleep 5
     puts "Deleting the Company :'#{record['CompanyName']}'"
     $ie.table(:id,"ctl00_ContentPlaceHolder1_grdCompanies")[i][7].button(:src,"http://"+$ip+"/CruxWebUI/appImages/delete.gif").click_no_wait
  
    sleep 2  
    hwnd = $ie.enabled_popup(5)

    if (hwnd)  #yeah! a popup
    popup = WinClicker.new
    popup.makeWindowActive(hwnd)
    popup.clickWindowsButton("Microsoft Internet Explorer", "OK", 30)
    end
 
    sleep 8
 
    puts "Company :'#{record['CompanyName']}'deleted successfully" 
    puts "Verifying the Deleted Company: '#{record['CompanyName']}'" 
    b=$ie.table(:id,"ctl00_ContentPlaceHolder1_grdCompanies").row_count

     for i in 2..b do
     a=$ie.table(:id,"ctl00_ContentPlaceHolder1_grdCompanies")[i][2].text 

    if a=="#{record['CompanyName']}"   
  
      d=1
      break
        end
          end 
     if d==1
     puts "company '#{record['CompanyName']}' not deleted successfully"
     else
     puts "Deleted company '#{record['CompanyName']}' verified successfully"
     end
      break  
       end   
        end
      end
      
      $ie.link(:text,"Home").click
      sleep 5

end

  #~ def test_05Logout
            #~ puts "Logging out from the Application"
            #~ $ie.link(:id,"ctl00_Logout_lnkLogout").click
            #~ puts "Successfully logged out from the Application"
            #~ puts "********************************"
            #~ puts "End of Companies Functionality"
            #~ puts "********************************"
            #~ sleep 5
            #~ $ie.close
  #~ end
  
  end


